package cn.stylefeng.guns.modular.web.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.ParamUtil;
import cn.stylefeng.guns.modular.rw.entity.EnterInfo;
import cn.stylefeng.guns.modular.rw.entity.InfoSet;
import cn.stylefeng.guns.modular.rw.service.*;
import cn.stylefeng.roses.core.base.controller.BaseController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述：企业资讯 controller
 * 作者：chenhuiyan
 * 时间：2020-05-08
 */
@Controller
@RequestMapping(value = "/web/enterInfo")
public class WebEnterInfoController extends BaseController {

    private String PREFIX = "/modular/web/";

    @Autowired
    private IInfoSetService infoSetService;

    @Autowired
    private IEnterInfoService enterInfoService;

    @Autowired
    private IBannerService bannerService;

    /**
     * 跳转到首页
     */
    @RequestMapping(value = "/")
    public String index() {
        return PREFIX + "enterInfo.html";
    }

    /**
     * 初始化数据
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getInitData")
    public Map<String, Object> getInitData(){
        Map<String, Object> res = new HashMap<>();

        Map<String, Object> params = ParamUtil.getParams();

        //查询基本配置
        InfoSet infoSet = infoSetService.getBaseMapper().selectList(new QueryWrapper<InfoSet>()).get(0);

        //banner企业资讯背景图
        params.put("webType", "2");
        Map<String, Object> banner = bannerService.list(params).getRecords().get(0);

        res.put("infoSet", infoSet);
        res.put("banner", banner);

        return res;
    }

    /**
     * 企业资讯列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getEnterInfoList")
    public Map<String, Object> getEnterInfoList(){

        Map<String, Object> res = new HashMap<>();

        Map<String, Object> params = ParamUtil.getParams();

        //企业资讯
        Page<Map<String, Object>> enterInfoPage = enterInfoService.list(params);
        LayuiPageInfo enterInfoList = LayuiPageFactory.createPageInfo(enterInfoPage);

        res.put("enterInfoList", enterInfoList);

        return res;
    }

    /**
     * 跳转到文章详情
     * @param model
     * @param enterInfoId
     * @return
     */
    @RequestMapping(value = "/articleDetail")
    public String articleDetail(Model model, String enterInfoId) {
        model.addAttribute("enterInfoId", enterInfoId);
        return PREFIX + "articleDetail.html";
    }

    /**
     * 获取企业资讯
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getEnterInfoById")
    public Map<String, Object> getEnterInfoById(String enterInfoId){

        Map<String, Object> res = new HashMap<>();

        Map<String, Object> enterInfo = enterInfoService.getInfoById(enterInfoId);

        res.put("enterInfo", enterInfo);

        return res;
    }

}
